#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from schema import RankComputation, Fetch, LinkGraphNode

import logging

ENCODE = "bz2"

class FetchFinder(webapp.RequestHandler):
  def get(self):
    body = '<form method="POST"><input type="submit" value="Encode" /></form>'
    self.response.out.write("<html><body>%s</body></html>" % body)
  
  def post(self):
    PAGESIZE = 5
    next = None
    bookmark = self.request.get("fetch_key")
    
    fetches = Fetch.all().order("__key__")
    if bookmark:
      logging.debug("This is a continuation.")
      logging.debug("The last batch left off with %s." % bookmark)
      old_fetches = fetches.filter('__key__ >=', db.Key(bookmark))
      batch_of_fetches = old_fetches.fetch(PAGESIZE+1)
    else:
      logging.debug("This is the first batch.")
      batch_of_fetches = fetches.fetch(PAGESIZE+1)

    logging.debug("This batch has %i fetches." % len(batch_of_fetches))

    if len(batch_of_fetches) == PAGESIZE+1:
      logging.debug("There are enough fetches for another batch.")
      next = batch_of_fetches[-1]
      logging.debug("The next batch starts with fetch %s." % str(next.key()))
      task = taskqueue.Task(
        url='/find-fetches-to-compress',
        params={
          'fetch_key': str(next.key()),
        }
      )
      task.add(queue_name='compress')
      batch_of_fetches = batch_of_fetches[:PAGESIZE]

    logging.debug("This batch has %i fetches." % len(batch_of_fetches))

    logging.debug("Now I'll make some tasks to do the compressing.")
    for fetch in batch_of_fetches:
      logging.debug("This fetch is encoded as %s" % fetch.encode)
      if fetch.encode != ENCODE:
        logging.debug("I'll make a task for fetch %s." % str(fetch.key()))
        task = taskqueue.Task(
          url='/compress-fetch',
          params={
            'fetch_key': str(fetch.key()),
            'encode': ENCODE,
          }
        )
        task.add(queue_name='compress')

def main():
  logging.getLogger().setLevel(logging.DEBUG)
  wsgiref.handlers.CGIHandler().run(webapp.WSGIApplication([
    ('/find-fetches-to-compress', FetchFinder),
  ]))


if __name__ == '__main__':
  main()
